package
{
	import asset.ConfigXML;
	import asset.GetMaterial;
	import asset.GetSWC;
	import asset.TimerManager;
	
	import com.Card;
	import com.CardEvent;
	
	import common.uis.card.newFontImage;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import model.CardModel;
	
	public class MatchingGame extends Sprite
	{
		private var _card1:Card = null;
		
		private var _card2:Card = null;
		
		private var _count:int = 0;
		
		public var _cardList:Array = [];
		
		private var _cardContainer:Sprite = null;
		
		private var _combineDict:Dictionary =  new Dictionary();
		
		public function MatchingGame()
		{
			super();
			
			// 支持 autoOrient
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			if (stage)
			{
				init();
			}
			else
			{
				this.addEventListener(Event.ADDED_TO_STAGE, addToStage);
			}
		}
		
		//加入到场景
		private function addToStage(e:Event):void 
		{
			
			removeEventListener(Event.ADDED_TO_STAGE, addToStage);
			init(); 
		}
		
		private var _arr3:Array = [];
		
		private function init():void
		{
			var swc:GetSWC = new GetSWC();
			
			TimerManager.getInstance().init(this.stage);
			
			var menu:GameMenu = new GameMenu();
			addChild(menu);
			
			
			
			_cardContainer = new Sprite();
			addChild(_cardContainer);
			var xml1:XML = ConfigXML.item as XML;
			var _arr3:Array = randomCard();
			
			this.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
			for(var i:int = 0;i<_arr3.length;i++)
			{
				var card:Card = new Card();
				_cardContainer.addChild(card);
				_cardList.push(card);
				card.addEventListener(CardEvent.SOUL_EXCHANGE,onClickCard);
				card.updateModel(_arr3[i],true);
				
				card.x = (250-card.width)/2 + (i%4)*(card.width+5);
				card.y = (300-card.height)/2 + int(i/4)*(card.height+5);
			}
		}
		
		private function onEnterFrameHandler(evt:Event):void
		{
			if(setOriginCardArr.length == 2)
			{
				var temp1:Card = setOriginCardArr[0] as Card;
				var temp2:Card = setOriginCardArr[1] as Card;
				
				if(temp1.isChoose && temp2.isChoose)
				{
					if(temp1.model.headId == temp2.model.headId)
					{
						var count:int = 0;
						while(count<_cardContainer.numChildren)
						{
							var card:Card = _cardContainer.getChildAt(count) as Card;
							if(card.model.id == temp1.model.id || card.model.id == temp2.model.id)
							{
								_cardContainer.removeChildAt(count);
							}
							else
							{
								count++;
							}
						}
					}
					else
					{
						temp1.startFlip(false);
						temp2.startFlip(false);
					}
					setOriginCardArr = [];
					this.mouseChildren = true;
					this.mouseEnabled = true;
				}
			}
		}
		
		private function onClickCard1(evt:CardEvent):void
		{
			trace(evt.target)
		}
		
		private function onClickCard(evt:CardEvent):void
		{
			if(evt.target is Card)
			{
				setOriginCardArr.push(evt.target);	
				if(setOriginCardArr.length == 2)
				{
					this.mouseEnabled = false;
					this.mouseChildren = false;;
				}
			}
		}
		
		private function countResult():void
		{
			if(_card1.model.headId == _card2.model.headId)
			{
				trace("Success");
				var count:int = 0;
				while(count<this.numChildren)
				{
					var card:Card = this.getChildAt(count) as Card;
					if(card.model.id == _card1.model.id || card.model.id == _card2.model.id)
					{
						this.removeChildAt(count);
					}
					else
					{
						count++;
					}
				}
			}
			else
			{
			}
		}
		
		private function conComplete(evt:Event):void
		{
			if(_card1.isChoose && _card2.isChoose)
			{
				_card1.startFlip(false);
				_card2.startFlip(false);
			}
		}
		
		private var setOriginCardArr:Array = [];
		
		private function randomCard():Array
		{
			var newArr:Array = [];
			var tempArr:Array = [];
			for(var i:int = 0;i<16;i++)
			{
				var model1:CardModel = ConfigXML.getItem(i+1);
				newArr.push(model1);
			}
			var arr2:Array = new Array();
			for (i=16; i>0;) {
				var rnd:int = Math.floor(Math.random()*i);
				arr2.push(newArr[rnd]);
				newArr[rnd] = newArr[--i];
			}
			return arr2;
		}
	}
}